import 'package:citiesocial/_include.dart';

class GoodsByActivity extends BaseStatefulWidget {
  final String type;
  GoodsByActivity(this.type);
  @override
  State<StatefulWidget> createState() => _GoodsByActivityState();
}

class _GoodsByActivityState extends BaseState<GoodsByActivity> {
  final _list = List<ActicityEntity>();
  var _page = 1;
  var _loadable = true;
  _GoodsByActivityState();

  void _loadData() async {
    LoadingDialog.show(context);
    final deviceId = await getDeviceId();
    final index =
        await HttpManager.shopActivity(widget.type, deviceId, page: _page);
    if (_page == 1) {
      _list.clear();
    }
    if (index?.name != null || ((index?.list?.length ?? 0) > 0)) {
      _loadable = index.list.length >= HttpManager.default_size;
      _list.addAll(index.list); //活动名为空，或者列表为空，则认为没返回有效数据
    } else {
      _loadable = false;
    }
    LoadingDialog.dismiss(context);
    setState(() {});
  }

  @override
  void initState() {
    Future.delayed(Configs.loadDelay, () {
      _loadData();
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: AppBarUtils.withCart(context, title: '更多闪购'),
        backgroundColor: Colors.white,
        body: SafeArea(
          child: DListView(
            onRefresh: () async {
              _loadable = true;
              _page = 1;
              _loadData();
            },
            onLoadmore: () {
              if (_loadable) {
                _page++;
                _loadData();
              }
            },
            itemCount: _list == null ? 0 : _list.length,
            itemBuilder: (_, index) => Container(
                  child: LongGoodsItemIndex(_list[index]),
                  margin: EdgeInsets.only(top: 10.0),
                ),
          ),
        ),
      );
}
